package cn.edu.ctbu.tydemo.webapi;


import cn.edu.ctbu.tydemo.core.PageUtils;
import cn.edu.ctbu.tydemo.domain.Student;
import cn.edu.ctbu.tydemo.domain.Teacher;
import cn.edu.ctbu.tydemo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@RestController
@RequestMapping("/webapi/teacher")
public class TeacherRestController {

    @Autowired
    private TeacherService teacherService;


    /**
     * 读取的url:"/webapi/teacher/list"
     * @return
     */
    @GetMapping("/list")
    public List<Teacher> getAll(){

        List<Teacher> teachers=teacherService.findAll();

        return teachers;
    }


    /**
     * 分页，读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name){

        //按id排降序

        Page<Teacher> teacherPage;
        if(StringUtils.isEmpty(name)) {
            Pageable pageable = PageRequest.of(page, size, Sort.by("id"));
            teacherPage = teacherService.findAll(pageable);
        }else{
            Teacher teacher=new Teacher();
            teacher.setName(name);

            Pageable pageable=PageRequest.of(0,10,Sort.by("id"));
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Teacher> example=Example.of(teacher,matcher);

            teacherPage=teacherService.findAll(example,pageable);
        }



        PageUtils pageUtils= new PageUtils(teacherPage.getContent(),teacherPage.getTotalElements());

        return pageUtils;

    }


    /**
     *读取的url:webapi/teacher/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Teacher get(@PathVariable Integer id){

        Teacher teacher=teacherService.findById(id);

        teacher.setPassword("");

        return teacher;
    }


    /**
     * 新增教师的方法，/webapi/teacher/insert
     * @param teacher
     * @return
     */
    @PostMapping("/insert")
    public Teacher insert(Teacher teacher){

        Teacher teacher1=teacherService.insert(teacher);

        return teacher1;

    }

    /**
     * 更新教师的方法，/webapi/teacher/update
     * @param teacher
     * @return
     */
    @PutMapping("/update")
    public Teacher update(Teacher teacher){

        //读取旧的数据
        Teacher oldteacher=teacherService.findById(teacher.getId());

        if(StringUtils.isEmpty(teacher.getPassword())){
            //没有旧密码，那么就是新密码
            teacher.setPassword(oldteacher.getPassword());

        }

        teacher= teacherService.update(teacher);

        return teacher;

    }


    /**
     * 删除教师的方法，webapi/teacher/delete
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable  Integer id){
        teacherService.delete(id);
    }


}
